﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using SysManage.BLL;
using ZHDJ.Core;
using ZHDJ.Core.Util;
using ZHDJ.Data;
using ZHDJ.Data.Models.Edu;
using ZHDJ.Data.Models.Grassroots;
using ZHDJ.WebPage;

namespace SysManage.Module.Grassroots
{
    public partial class ItemMemberEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "GrassrootsGroupAdd";
            }
        }
        public string ItemID
        {
            get
            {
                var temp = GetQueryIntValue("ItemID");
                return string.IsNullOrEmpty(temp) ? "" : temp;
            }
        }
        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        public void BindData()
        {
            ControlBusiness.ControlBindDataDictionary(
               ddlCategory, SysDataDictionary.GrassrootsDutyCategory);
        }

        private void LoadData()
        {
            var itemId = "";
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyGrassrootsGroupItemMember current = DB.PartyGrassrootsGroupItemMembers.Find(id);
                if (current == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                itemId = current.ItemID;
                txtMobileNo.Text = current.MobileNo;
                ddlCategory.SelectedValue = current.DutyCategory;
                txtPost.Text = current.Post;
                txtOrderNo.Text = current.OrderNo.ToString();
                imgPhoto.ImageUrl = UpFileBusiness.GetFileUrl(current.ImageID);
                ppMember.Text = ControlBusiness.GetMembersPickerText(current.LoginID);
                ppMember.Value = current.LoginID;
            }
            else
            {
                itemId = ItemID;
                if (itemId == "")
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                
            }
            var item = DB.PartyGrassrootsGroupItems.Find(itemId);
            if (item == null)
            {
                // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                return;
            }
            this.ppMember.WindinsUrl = "../../../Share/Picker/MemberSinglePicker.aspx?BranchID=" + item.BranchID;
            txtItemName.Text = item.ItemName;
        }

        #endregion

        #region Events

        private void SaveData()
        {
            PartyGrassrootsGroupItemMember item;
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                //更新
                item = DB.PartyGrassrootsGroupItemMembers.Find(id);
                if (item == null)
                {
                    Alert.Show("参数错误！", String.Empty);
                    return;
                }
                item.ModifyTime = DateTime.Now;
                item.ModifyBy = CurrentUser.LoginID;
            }
            else
            {
                var current = DB.PartyGrassrootsGroupItemMembers.Where(a => a.LoginID == ppMember.Value);
                if (current.Any())
                {
                    Alert.Show("成员已存在，请重新选择！", String.Empty);
                    return;
                }
                item = new PartyGrassrootsGroupItemMember();
                item.ID = StringHelper.NewID;
                item.ItemID = ItemID;
                item.CreateTime = DateTime.Now;
                item.ModifyTime = DateTime.Now;
                item.CreateBy = CurrentUser.LoginID;
                item.ModifyBy = CurrentUser.LoginID;
                item.DutyCategory = ddlCategory.SelectedValue;
                DB.PartyGrassrootsGroupItemMembers.Add(item);
            }
            item.LoginID = ppMember.Value;
            item.UserName = ppMember.Text;
            item.MobileNo = txtMobileNo.Text;
            item.Post = txtPost.Text;
            item.DutyCategory = ddlCategory.SelectedValue;
            item.OrderNo = Convert.ToInt32(txtOrderNo.Text);
            if (!string.IsNullOrEmpty(hdFileId.Text))
            {
                item.ImageID = hdFileId.Text;
            }
            DB.SaveChanges();
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();

        }
        #endregion

        protected void filePhoto_FileSelected(object sender, EventArgs e)
        {
            var rs = UpFileBusiness.Upload(filePhoto, CurrentUser.LoginID, "GrassrootsMember-image", SysEnum.UploadFileType.uploadimage);
            if (rs.IsSuccess)
            {
                hdFileId.Text = rs.FileID;
                imgPhoto.ImageUrl = UpFileBusiness.GetFileUrl(hdFileId.Text);
                Alert.Show("上传成功！", String.Empty);
            }
            else
            {
                Alert.Show("上传失败！" + rs.Message, String.Empty);
            }
        }
    }
}
